<template>
  <div class="bgMain">
    <div class="formCon">
      <h4>注册信息</h4>
      <el-form
        ref="ruleForm2"
        :model="ruleForm2"
        :rules="rules2"
        status-icon
        label-width="80px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="ruleForm2.username" type="text"/>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="ruleForm2.password" type="password" auto-complete="off"/>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
          <el-input v-model="ruleForm2.checkPass" type="password" auto-complete="off"/>
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model.number="ruleForm2.phone"/>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm2')">提交</el-button>
          <el-button @click="resetForm('ruleForm2')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import { addUser } from '@/api/user'
export default {
  data() {
    var checkPhone = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('手机号'))
      }
      setTimeout(() => {
        if (!Number.isInteger(value)) {
          callback(new Error('请输入数字值'))
        } else {
          if (value.length > 10) {
            callback(new Error('手机号不正确'))
          } else {
            callback()
          }
        }
      }, 1000)
    }
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        if (this.ruleForm2.checkPass !== '') {
          this.$refs.ruleForm2.validateField('checkPass')
        }
        callback()
      }
    }
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.ruleForm2.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      ruleForm2: {
        password: '',
        checkPass: '',
        phone: '',
        username: ''
      },
      rules2: {
        password: [{ validator: validatePass, trigger: 'blur' }],
        checkPass: [{ validator: validatePass2, trigger: 'blur' }],
        phone: [{ validator: checkPhone, trigger: 'blur' }]
      }
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          const obj = {}
          obj.form = this.ruleForm2
          // console.log(JSON.parse(JSON.stringify(obj)))
          addUser(obj).then((result) => {
            if (result.success) {
              this.$router.push({
                path: '/home'
              })
            }
          }).catch((err) => {
            console.log(err)
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>
<style lang="scss" scoped>
.bgMain {
  width: 100%;
  height: 100%;
  margin: 0 auto;
  position: relative;
  background: url(https://fes.qyerstatic.com/fe_ssr_passport/static/web_login_bg.ecb39528.jpg) no-repeat;
  background-size: cover;
  padding-top: 100px;
  .formCon {
    border-radius: 3px;
    width: 600px;
    height: 500px;
    background: hsla(0, 0%, 100%, 0.95);
    margin: 0 auto;
    text-align: center;
    padding: 30px 20px;
    padding-left: 10px;
    border: solid 1px #ebebeb;
    box-sizing: border-box;
  }
}
</style>
